(function (vc) {
    var DEFAULT_ROWS = 10
    vc.extends({
        data: {
            addAStaffCommunityInfo: {
                communitys: [],
                communityName: '',
                staffId: '',
                staffName: '',
                selectCommunitys: []
            }
        },
        watch: { // 监视双向绑定的数据数组
            checkData: {
                handler() { // 数据数组有变化将触发此函数
                    if ($that.addAStaffCommunityInfo.selectCommunitys.length == $that.addAStaffCommunityInfo.communitys.length) {
                        document.querySelector('#quan').checked = true;
                    } else {
                        document.querySelector('#quan').checked = false;
                    }
                },
                deep: true // 深度监视
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('addAStaffCommunity', 'openAddAStaffCommunityModal', function (_param) {
                $that._refreshChooseCommunityInfo();
                $('#addAStaffCommunityModel').modal('show');
                vc.copyObject(_param, $that.addAStaffCommunityInfo);
                $that._loadAllCommunityInfo(1, 10, '');
            });
            vc.on('addAStaffCommunity', 'paginationPlus', 'page_event', function (_currentPage) {
                $that._loadAllCommunityInfo(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _loadAllCommunityInfo: function (_page, _row, _name) {
                let param = {
                    params: {
                        page: _page,
                        row: _row,
                        nameLike: _name,
                        staffId: $that.addAStaffCommunityInfo.staffId
                    }
                };
                //发送get请求
                vc.http.apiGet('/role.listWaitAStaffCommunity',
                    param,
                    function (json) {
                        let _json = JSON.parse(json);
                        $that.addAStaffCommunityInfo.communitys = _json.data;
                        vc.emit('addAStaffCommunity', 'paginationPlus', 'init', {
                            total: _json.records,
                            dataCount: _json.total,
                            currentPage: _page
                        });
                    }, function () {
                        console.log('请求失败处理');
                    }
                );
            },
            addAStaffCommunity: function (_org) {
                let _selectCommunitys = $that.addAStaffCommunityInfo.selectCommunitys;
                let _tmpCommunitys = $that.addAStaffCommunityInfo.communitys;
                if (_selectCommunitys.length < 1) {
                    vc.toast("请选择小区");
                    return;
                }
                var _communitys = [];
                for (var _selectIndex = 0; _selectIndex < _selectCommunitys.length; _selectIndex++) {
                    for (var _communityIndex = 0; _communityIndex < _tmpCommunitys.length; _communityIndex++) {
                        if (_selectCommunitys[_selectIndex] == _tmpCommunitys[_communityIndex].communityId) {
                            _communitys.push({
                                communityId: _tmpCommunitys[_communityIndex].communityId,
                                communityName: _tmpCommunitys[_communityIndex].name
                            });
                        }
                    }
                }
                let _objData = {
                    staffId: $that.addAStaffCommunityInfo.staffId,
                    staffName: $that.addAStaffCommunityInfo.staffName,
                    communitys: _communitys
                }
                vc.http.apiPost('/role.saveStaffCommunity',
                    JSON.stringify(_objData),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        $('#addAStaffCommunityModel').modal('hide');
                        let _json = JSON.parse(json)
                        vc.toast(_json.msg);

                        if (_json.code == 0) {
                            $that.addAStaffCommunityInfo.selectCommunitys = [];
                            vc.emit('aStaffCommunity', 'listAStaffCommunity', {});
                            vc.emit('aStaffDetailCommunitys', 'notify', {});
                            vc.emit('staffDetailCommunitys', 'notify', {});
                            
                            return;
                        }
                    }, function () {
                        console.log('请求失败处理');
                    }
                );
                // $('#addAStaffCommunityModel').modal('hide');
            },
            //查询
            queryCommunitys: function () {
                $that._loadAllCommunityInfo(1, 10, $that.addAStaffCommunityInfo.communityName);
            },
            //重置
            resetCommunitys: function () {
                $that.addAStaffCommunityInfo.communityName = "";
                $that._loadAllCommunityInfo(1, 10, $that.addAStaffCommunityInfo.communityName);
            },
            _refreshChooseCommunityInfo: function () {
                $that.addAStaffCommunityInfo = {
                    communitys: [],
                    communityName: '',
                    staffId: '',
                    staffName: '',
                    selectCommunitys: []
                };
            },
            checkAllCommunity: function (e) {
                var checkObj = document.querySelectorAll('.checkItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.addAStaffCommunityInfo.selectCommunitys.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.addAStaffCommunityInfo.selectCommunitys = [];
                }
            },
            isCheckAll: function (e) {
                let checkObj = document.querySelectorAll('.checkItem');
                var count = 0;
                for (var i = 0; i < checkObj.length; i++) {
                    if (checkObj[i].checked) {
                        count++;
                    }
                }
                if (checkObj.length == count) {
                    document.getElementById("quan").checked = true;
                } else {
                    document.getElementById("quan").checked = false;
                }
            }
        }
    });
})(window.vc);
